package gwtappcontainer.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

import gwtappcontainer.client.apps.IPresenter;
import gwtappcontainer.shared.ViewProp;


public class ContainerPresenter {
	private ContainerView containerView;
	
	StatsServiceAsync statsSvc = GWT.create(StatsService.class);
	
	public ContainerPresenter(ContainerView v) {
		containerView = v;
	}
		
	public void setTitle(String title) {
		containerView.titleLabel.setText(title);		
	}
	
	public void setUser(String email) {
		containerView.userId.setInnerText(email);
	}
	
	public void setSignoutLink(String url) {
		containerView.signInLink.setHref(url);
		containerView.signInLink.setText("Sign out");
	}
	
	public void setSigninLink(String url) {
		containerView.signInLink.setHref(url);
		containerView.signInLink.setText("Sign in with google or isha account");
	}	
	
	public void setStatus(String status) {
		containerView.status.setInnerText(status);		
	}
			
	public void loadView(ViewProp viewDesc) {
				
		setTitle(Constants.containerTitle + " - loading " + viewDesc.app + "...");
		
		//TODO - put a gwt code split point here
		IPresenter presenter = new PresenterFactory().
				getPresenter(viewDesc.app, viewDesc.view);
		
		presenter.render(containerView.contentFlowPanel, viewDesc.params);			
				
		setTitle(Constants.containerTitle + " - " + viewDesc.app);
		
		statsSvc.recordView(viewDesc, new AsyncCallback<Void>() {			
			@Override
			public void onSuccess(Void result) {
				// leave empty				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// leave empty				
			}
		});		
	}		
}
